package com.caojing.haolemai.ui.cart;


import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.caojing.haolemai.MyApp;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.db.CartBean;
import com.caojing.haolemai.ui.product.ProductActivity;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class CartFragment extends BaseFragment {


    @Bind(R.id.cart_ll)
    LinearLayout cartLl;
    @Bind(R.id.cart_lv)
    ListView cartLv;
    @Bind(R.id.cart_cbTotal)
    CheckBox cartCbTotal;
    @Bind(R.id.cart_tvTotal)
    TextView cartTvTotal;
    @Bind(R.id.cart_tvPay)
    TextView cartTvPay;
    private List<CartBean> mlist;
    private CartLvAdapter adapter;
    public CartFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cart;
    }
    @Override
    public void onResume() {
        super.onResume();
        try {
            MyApp.dbUtils2.createTableIfNotExist(CartBean.class);
            mlist= MyApp.dbUtils2.findAll(CartBean.class);
            adapter = new CartLvAdapter(mlist,getContext());
            cartLv.setAdapter(adapter);
        } catch (DbException e) {
            e.printStackTrace();
        }
        cartLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String pid = mlist.get(i).getPid();
                Intent intent = new Intent(getContext(), ProductActivity.class);
                intent.putExtra("id",pid);
                startActivity(intent);
            }
        });
        cartLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("确定要从收藏夹中删除吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                try {
                                    MyApp.dbUtils2.delete(CartBean.class, WhereBuilder.b("pid","=",mlist.get(position).getPid()));
                                    mlist.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).setNegativeButton("取消",null)
                        .show();
                return true;
            }
        });
    }
    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {

    }



    @OnClick({R.id.cart_ll, R.id.cart_cbTotal, R.id.cart_tvPay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cart_ll:
                break;
            case R.id.cart_cbTotal:
                break;
            case R.id.cart_tvPay:
                Toast.makeText(getContext(), "未安装支付宝", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
